﻿using L.EntityFramework;
using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using NB.MGJ.Model.CodeManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.CodeManage
{
  public  class ApplyCodeService:BaseService
  {
      public BaseRepository _baseRepository = new BaseRepository();

      private BaseRepository<L.EntityModel.EntityContext> baserReposityory;
      public ApplyCodeService()
      {
          baserReposityory = CreateBaseRepository<L.EntityModel.EntityContext>();
      }

      public PagedList<L.EntityModel.CodeManage_Allot> GetApplyListService(PagerModel pager, int enterpriseId, DetailSearch Smodel)
      {

          var Query = baserReposityory.Search<L.EntityModel.CodeManage_Allot>(p=>p.T_EnterpriseId == enterpriseId);
          if (Smodel.ApplyCount > 0)
          {
              Query = Query.Where(p => p.ApplyCount == Smodel.ApplyCount);
          }
          if (!string.IsNullOrWhiteSpace(Smodel.SerialNum))
          {
              Query = Query.Where(p => p.SerialNum.Contains(Smodel.SerialNum));
          }
          if (!string.IsNullOrWhiteSpace(Smodel.ApplyProduct))
          {
              Query = Query.Where(p=>p.ProductName.Contains(Smodel.ApplyProduct));
          }

          if (Smodel.StartDate != "" && Smodel.StartDate != null)
          {
              DateTime sTime;
              if (DateTime.TryParse(Smodel.StartDate, out sTime))
              {
                  Query = Query.Where(p => p.ApplyDate >= sTime);
              }
          }
          if (Smodel.EndDate != "" && Smodel.EndDate != null)
          {
              DateTime eTime;
              if (DateTime.TryParse(Smodel.EndDate, out eTime))
              {
                  Query = Query.Where(p => p.ApplyDate <= eTime);
              }
          }

          Query = Query.OrderBy(p => p.HasVerifie);
          return new PagedList<L.EntityModel.CodeManage_Allot>(Query, pager.Page, pager.PageSize);

          #region MyRegion
          //pager.FldName = "*";
          //pager.TblName = "codemanage_allot";
          //string StrCondition = "   1=1 ";

          //   if (string.IsNullOrWhiteSpace(pager.StrCondition))
          //   {
          //       StrCondition = "   1=1 ";
          //    }
          //    else
          //    {
          //        StrCondition = pager.StrCondition;
          //    }
          //   if (Smodel.ApplyCount != 0)
          //   {
          //       StrCondition += "  and (ApplyCount like '%" + Smodel.ApplyCount + "%')";
          //   }
          //   if (Smodel.SerialNum!=null)
          //   {
          //       StrCondition += " and (SerialNum like '%" + Smodel.SerialNum + "%')";
          //   }
          //   if (Smodel.ApplyProduct!=null)
          //   {
          //       StrCondition +=" and (AllotProductName like '%" + Smodel.ApplyProduct + "%')";
          //   }

          //    if (Smodel.StartDate != "" && Smodel.StartDate != null)
          //    {
          //        StrCondition += " and  (left(ApplyDate,10) >= '" + Smodel.StartDate + "')";
          //    }
          //    if (Smodel.EndDate != "" && Smodel.EndDate != null)
          //    {
          //       StrCondition += " and  (left(ApplyDate,10) <= '" + Smodel.EndDate + "')";
          //    }
          //    StrCondition += string.Format(" and T_EnterpriseId={0}", enterpriseId);   
          //pager.StrCondition = StrCondition;
          //pager.FldSort = "HasVerifie ASC";

          //MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);
          //IEnumerable<CodeManage_Allot> resultList = _baseRepository.ExecuteStoredProcedureCollection<CodeManage_Allot>("Common_PageList", parameters);
          //return new PagedList<CodeManage_Allot>(resultList, pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value)); 
          #endregion
      }

      //public PagedList<CodeManage_Allot> GetApplyListService(ref PagerModel pager, DetailSearch Smodel)
      //{

      //    pager.FldName = @"*";
      //    pager.TblName = @"CodeManage_Allot";
      //    string StrCondition = "   1=1 ";
      //    if (string.IsNullOrWhiteSpace(pager.StrCondition))
      //    {
      //        StrCondition = "   1=1 ";
      //    }
      //    else
      //    {
      //        StrCondition = pager.StrCondition;
      //    }

      //    if (Smodel != null)
      //    {
      //        StrCondition += "  and (BrandName like '%" + Smodel.ApplyBrand + "%')and  (SerialNum like '%" + Smodel.SerialNum + "%') and (AllotProductName like '%" + Smodel.ApplyProduct + "%') ";
      //    }
      //    if (Smodel.StartDate != "" && Smodel.StartDate != null)
      //    {
      //        StrCondition += " and  (left(ApplyDate,10) >= '" + Smodel.StartDate + "')";
      //    }
      //    if (Smodel.EndDate != "" && Smodel.EndDate != null)
      //    {
      //        StrCondition += " and  (left(ApplyDate,10) <= '" + Smodel.EndDate + "')";
      //    }
      //    pager.StrCondition = StrCondition;
      //    if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == "")
      //        pager.FldSort = " ApplyDate desc ";

      //    MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);
      //    IEnumerable<CodeManage_Allot> resultList = _baseRepository.ExecuteStoredProcedureCollection<CodeManage_Allot>("Common_PageList", parameters);
      //    return new PagedList<CodeManage_Allot>(resultList, pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
      //}

      public List<Organization_Enterprise> GetentService(int EnterpriseId)
      {
          string sql = "";
          if (EnterpriseId == 1)
          {

              sql = "select ID, Code, EnterpriseName, OrganizationForm, OrganizationId, SuperiorsOrganization, ProvinceId, City, Region, Address, StartTime, EndTime, PersonCharge, PersonMobile, LandlinePhone, JurisdictionRegion, Img, Remark, ViewDel, ViewFlag, ChangeLoginID, ChangeLoginName, ChangeDate, CreateDate from  Organization_Enterprise where ViewDel=0 ";
          }
          else
          {
              sql = "select ID, Code, EnterpriseName, OrganizationForm, OrganizationId, SuperiorsOrganization, ProvinceId, City, Region, Address, StartTime, EndTime, PersonCharge, PersonMobile, LandlinePhone, JurisdictionRegion, Img, Remark, ViewDel, ViewFlag, ChangeLoginID, ChangeLoginName, ChangeDate, CreateDate from  Organization_Enterprise where ViewDel=0 and ID=" + EnterpriseId;
          }
          return _baseRepository.ExecuteSqlCollection<Organization_Enterprise>(sql).ToList();
      }

      public List<Product_Product> GetproService(int EId)
      {
          string sql = "select ID,ProductName from  Product_Product where ViewDel=0 and EnterpriseId=" + EId;
          return _baseRepository.ExecuteSqlCollection<Product_Product>(sql).ToList();
      }

      public int AddApplyService(L.EntityModel.CodeManage_Allot model)
      {
          int rulset = baserReposityory.Insert<L.EntityModel.CodeManage_Allot>(model);
          return rulset;
      }

      /// <summary>
      /// 取消/删除 码申请操作
      /// </summary>
      public void CancelApply(int id)
      {
          string _sql = string.Format("DELETE FROM codemanage_allot WHERE ID = {0}  AND HasVerifie = 0", id);
          _baseRepository.ExecuteSql<int>(_sql);
      }
    }
}
